package de.scs.core;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.List;
import java.util.TimerTask;

import de.scs.constants.Status;

public  class BSLTask extends TimerTask
{

  private List<Integer>	   bsvList;
  private boolean	     stopped;
  private java.util.Timer	 timer;
  private  int	  bsv         = 0;
  private  PropertyChangeSupport timerStatus = new PropertyChangeSupport(BSLTask.class);

  public BSLTask(List<Integer> bsvList,  java.util.Timer timer)
  {
    this.bsvList = bsvList;
    this.timer = timer;
  }

  @Override
  public void run()
  {
    if (stopped)
    {
      timer.cancel();
       HealthStatusController.GetInstance().logger.info("Reading from file finished.Timer stopped!");
    }

    if (HealthStatusController.GetInstance().getCurrentStatus() != Status.ERROR)
    {
      int currentValue = bsvList.get(bsv);
      HealthStatusController.GetInstance().getInsulinPump().setBloodSugarValue(currentValue);
       HealthStatusController.GetInstance().logger.info("current blood sugar value - " + currentValue);
      bsv++;
      if (bsv == bsvList.size())
      {
        boolean lastStatus = stopped;
        stopped = true;
        timerStatus.firePropertyChange("stopped", lastStatus, stopped);
        bsv = 0;
      }
    }
  }

  public boolean isStopped()
  {
    return stopped;
  }

  public void setStopped(boolean stopped)
  {
    this.stopped = stopped;
  }

  public  void addPropertyChangeListener(PropertyChangeListener l)
  {
    timerStatus.addPropertyChangeListener(l);
  }

  public  void removePropertyChangeListener(PropertyChangeListener l)
  {
    timerStatus.removePropertyChangeListener(l);
  }
}


